/**
 * Copyright (C) 2011-2021 liepin Inc.All Rights Reserved.
 * 
 * FileName:UserOrderServiceImpl.java
 *
 * Description：简要描述本文件的内容
 *
 * History：
 * 版本号           作者                  日期               简要介绍相关操作
 *  1.0  chenyong  2016年11月8日
 *
 */
package com.buybuyyi.stylistcrm.order.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.buybuyyi.stylistcrm.consts.DateFormatPattern;
import com.buybuyyi.stylistcrm.order.helper.ConsigneeHelper;
import com.buybuyyi.stylistcrm.order.helper.OrderGoodsHelper;
import com.buybuyyi.stylistcrm.order.po.Consignee;
import com.buybuyyi.stylistcrm.order.po.UserOrder;
import com.buybuyyi.stylistcrm.order.po.UserOrderGoods;
import com.buybuyyi.stylistcrm.utils.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.buybuyyi.stylistcrm.order.dao.ConsigneeDao;
import com.buybuyyi.stylistcrm.order.dao.UserOrderDao;
import com.buybuyyi.stylistcrm.order.dao.UserOrderGoodsDao;

import stylistcrm.core.query.ResultPagerModel;
import stylistcrm.core.services.order.dto.OrderGoodsDto;
import stylistcrm.core.services.order.dto.PurchaseOrderDto;
import stylistcrm.core.services.order.dto.QueryOrderConditionDto;
import stylistcrm.core.services.order.dto.UserOrderDto;
import stylistcrm.core.services.order.enums.EnumExceptionCode;
import stylistcrm.core.services.order.exception.BizException;
import stylistcrm.core.services.order.service.UserOrderService;

/**
 * @author chenyong
 *
 */
@Service
public class UserOrderServiceImpl implements UserOrderService {

	@Autowired
	private UserOrderDao userOrderDao;
	
	@Autowired
	private UserOrderGoodsDao userOrderGoodsDao;
	
	@Autowired
	private ConsigneeDao consigneeDao;

	/**
	 * 创建用户订单
	 * @param userOrder
	 * @return
	 * @throws BizException
     */
	@Override
	public Boolean createUserOrder(UserOrderDto userOrder) throws BizException {
		if(null == userOrder){
			throw new BizException(EnumExceptionCode.EC0001);
		}
		Consignee consignee= ConsigneeHelper.getOrderConsignee(userOrder);
		if(null == consignee){
			throw new BizException(EnumExceptionCode.EC0002);
		}
		if(consignee.getId()!=null){
			consigneeDao.updateByPrimaryKeySelective(consignee);
		}else{
			Integer consigneeId=consigneeDao.insertSelective(consignee);
			userOrder.setConsigneeId(consigneeId);
		}
		OrderGoodsHelper.setGoodsList(userOrder);
		UserOrder userOrderEntity=convertUserOrderDto2Entity(userOrder);
		if(null!=userOrderEntity){
			Integer ret=userOrderDao.insertSelective(userOrderEntity);
			if(ret<=0){
				throw new BizException(EnumExceptionCode.EC0008);
			}
			List<OrderGoodsDto> orderGoodsDtoList=userOrder.getGoods();
			if(null==orderGoodsDtoList||orderGoodsDtoList.isEmpty()){
				throw new BizException(EnumExceptionCode.EC0009);
			}
			for(OrderGoodsDto orderGoodsDto:orderGoodsDtoList){
				UserOrderGoods userOrderGoods=convertUserOrderGoodsDto2Entity(orderGoodsDto);
				if(null==userOrderGoods){
					continue;
				}
				userOrderGoods.setUserOrderId(userOrderEntity.getId());
				userOrderGoodsDao.insertSelective(userOrderGoods);
			}
		}
		return Boolean.TRUE;
	}

	/**
	 * 用户订单dto转entity保存入库
	 * @param userOrderDto
	 * @return
     */
	private UserOrder convertUserOrderDto2Entity(UserOrderDto userOrderDto){
		if(null == userOrderDto){
			return null;
		}
		UserOrder userOrder=new UserOrder();
		userOrder.setConsigneeId(userOrderDto.getConsigneeId());
		userOrder.setUserId(userOrderDto.getUserId());
		userOrder.setModifierId(userOrderDto.getModifierId());
		userOrder.setAuditStatus(userOrderDto.getAuditStatus());
		if(null!=userOrderDto.getModifyTime()){
			userOrder.setModifyTime(DateUtil.parseDate(userOrderDto.getModifyTime(), DateFormatPattern.PATTERN_YYYY_MM_DD_HHMMSS));
		}
		userOrder.setOrderStatus(userOrderDto.getOrderStatus());
		if(null!=userOrderDto.getOrderTime()){
			userOrder.setOrderTime(DateUtil.parseDate(userOrderDto.getOrderTime(),DateFormatPattern.PATTERN_YYYY_MM_DD_HHMMSS));
		}else{
			userOrder.setOrderTime(new Date());
		}
		userOrder.setPayStatus(userOrderDto.getPayStatus());
		if(null!=userOrderDto.getPayTime()){
			userOrder.setPayTime(DateUtil.parseDate(userOrderDto.getPayTime(),DateFormatPattern.PATTERN_YYYY_MM_DD_HHMMSS));
		}
		userOrder.setPayWay(userOrderDto.getPayWay());
		userOrder.setTotalPrice(userOrderDto.getTotalPrice());
		userOrder.setDelFlag(userOrderDto.getDelFlag());
		return userOrder;
	}

	/**
	 * 用户订单商品转换entity入库
	 * @param orderGoodsDto
	 * @return
     */
	private UserOrderGoods convertUserOrderGoodsDto2Entity(OrderGoodsDto orderGoodsDto){
		if(null==orderGoodsDto){
			return null;
		}
		UserOrderGoods userOrderGoods=new UserOrderGoods();
		userOrderGoods.setColor(orderGoodsDto.getColor());
		userOrderGoods.setCount(orderGoodsDto.getCount());
		userOrderGoods.setCreateTime(new Date());
		userOrderGoods.setDesignerId(orderGoodsDto.getDesignerId());
		userOrderGoods.setGoodsId(orderGoodsDto.getGoodsId());
		userOrderGoods.setGoodsName(orderGoodsDto.getGoodsName());
		userOrderGoods.setSalePrice(orderGoodsDto.getGoodsSalePrice());
		userOrderGoods.setOrderPrice(orderGoodsDto.getOrderPrice());
		userOrderGoods.setSize(orderGoodsDto.getSize());
		userOrderGoods.setTotalPrice(orderGoodsDto.getTotalPrice());
		userOrderGoods.setYardageId(orderGoodsDto.getYardageId());
		return userOrderGoods;
	}

	/**
	 * 根据主键查询用户订单
	 * @param id
	 * @return
     */
	@Override
	public UserOrderDto selectByPrimaryKey(Integer id) {
		QueryOrderConditionDto queryCondition=new QueryOrderConditionDto();
		List<Integer> ids=new ArrayList<Integer>();
		ids.add(id);
		queryCondition.setIds(ids);
		List<UserOrderDto> userOrderList=userOrderDao.getPageList(queryCondition);
		if(null!=userOrderList&&0!=userOrderList.size()){
			return userOrderList.get(0);
		}
		return null;
	}

    /**
     * 分页查询用户订单
     * @param queryCondition 查询条件
     * @return
     */
	@Override
	public ResultPagerModel<UserOrderDto> getPageList(QueryOrderConditionDto queryCondition) {
		Integer currentPage=queryCondition.getCurrentPage();
		Integer pageSize=queryCondition.getPageSize();
		if(currentPage==null){
			currentPage=1;
			queryCondition.setCurrentPage(currentPage);
		}
		if(pageSize==null){
			pageSize=15;
			queryCondition.setPageSize(pageSize);
		}
		queryCondition.setStart((currentPage-1)*pageSize);
		ResultPagerModel<UserOrderDto> resultPager=new ResultPagerModel<UserOrderDto>();
		resultPager.setCurrentPage(currentPage);
		resultPager.setPageSize(pageSize);
		List<UserOrderDto> userOrderList=userOrderDao.getPageList(queryCondition);
		Integer totalCount=getTotalCount(queryCondition);
		resultPager.setTotalServerItems(totalCount);
		resultPager.setData(userOrderList);
		return resultPager;
	}

    /**
     * 根据条件获取所有的订单数
     * @param queryCondition
     * @return
     */
	@Override
	public Integer getTotalCount(QueryOrderConditionDto queryCondition) {
		return userOrderDao.getTotalCount(queryCondition);
	}

}
